package cn.cdiothree.controller;

import cn.cdiothree.DTO.OrderDTO;
import cn.cdiothree.dataobject.ProductCategory;
import cn.cdiothree.dataobject.ProductInfo;
import cn.cdiothree.exception.SellException;
import cn.cdiothree.form.ProductForm;
import cn.cdiothree.service.ProductCategoryService;
import cn.cdiothree.service.ProductInfoService;
import cn.cdiothree.service.ProductOrderService;
import cn.cdiothree.util.KeyUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 卖家端
 * Create by jocelyn
 *
 */
@Controller
@RequestMapping("/seller/product")
public class SellerProductController {

    @Autowired
    private ProductInfoService productInfoService;

    @Autowired
    private ProductCategoryService productCategoryService;

    /**
     * 列表
     * @param page
     * @param size
     * @param map
     * @return
     */
    @GetMapping("/list")
    public ModelAndView list(@RequestParam(value = "page",defaultValue = "1") Integer page,
                             @RequestParam(value = "size",defaultValue = "10") Integer size,
                             Map<String,Object> map){
        IPage<ProductInfo> productInfoPage=productInfoService.findAll(page-1,size);
        map.put("productInfoPage",productInfoPage);
        map.put("currentPage",page);
        map.put("size",size);
        return new ModelAndView("product/list",map);
    }

    /**
     * 商品列表主页
     * @param id
     * @param map
     * @return
     */
    @GetMapping("/index")
    public ModelAndView index(@RequestParam(value = "id",required =false) Integer id,
                      Map<String,Object> map){
        if (!StringUtils.isEmpty(id)){
            ProductInfo productInfo=productInfoService.findById(id);
            map.put("productInfo",productInfo);
        }
        //查询所有类目
        List<ProductCategory> productCategoryList= productCategoryService.findAll();
        map.put("productCategoryList",productCategoryList);
        return new ModelAndView("product/index",map);
    }

    /**
     * 保存/更新
     * @param form
     * @param bindingResult
     * @param map
     * @return
     */
    @PostMapping("/save")
    public ModelAndView save(@Valid ProductForm form,
                             BindingResult bindingResult,
                             Map<String,Object> map){
        if (bindingResult.hasErrors()){
            map.put("msg",bindingResult.getFieldError().getDefaultMessage());
            map.put("url","/cumthing/seller/product/index");
            return new ModelAndView("common/error",map);
        }

        ProductInfo productInfo=new ProductInfo();
        try {
            //如果productId不为空，说明是新增
            if (!StringUtils.isEmpty(form.getId())){
                productInfo=productInfoService.findById(form.getId());
                BeanUtils.copyProperties(form,productInfo);
                productInfoService.updateById(productInfo);
            }else {
                BeanUtils.copyProperties(form,productInfo);
                productInfoService.save(productInfo);
            }

        }catch (SellException e){
            map.put("msg",e.getMessage());
            map.put("url","/cumthing/seller/product/index");
            return new ModelAndView("common/error",map);
        }
        map.put("url","/cumthing/seller/product/list");
        return new ModelAndView("common/success",map);

    }
}
